/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.component.jtree.OverwriteDefaultMutableTreeNode;
/**
 * 说明：
 * 商品分类选择页面
 * @Author：李智深
 * @Date：2012-03-14
 */
public class DataTProductCatalogSelectDialog extends IDialog implements DataCommodityQuery{

	private static final long serialVersionUID = 1L;
	
	private DataTProductCatalogTreePanel dataTProductCatalogTreePanel; 
	private JButton noButton; //取消按钮
	private JButton yesButton;//确定按钮
	private OverwriteDefaultMutableTreeNode treeNode;
	private int nodeRow;//第几行
	
	private int queryAt = 0;//分类查询至第几条数据
	private JPanel waitComponentJPanel;
	private JProgressBar jpb;//进度条
	
	public DataTProductCatalogSelectDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		
		jpb = new JProgressBar(0, 100);
		Util.setSize(jpb, 93, 15);
		
		waitComponentJPanel = new BorderJPanel(){
			private static final long serialVersionUID = 1L;
			private Color color = new Color(246, 163, 49);
			@Override
			protected void paintBorder(Graphics g) {
				g.setColor(color);
				g.fillRect(0, 0, 4, this.getHeight());
				g.fillRect(this.getWidth()-4, 0, 4, this.getHeight());
			}
		};
		BoxLayout boxLayout = new BoxLayout(waitComponentJPanel, BoxLayout.Y_AXIS);
		waitComponentJPanel.setLayout(boxLayout);
		waitComponentJPanel.add(new JLabel("数据加载中……", JLabel.LEFT));
		waitComponentJPanel.add(jpb);
		waitComponentJPanel.setBackground(Util.PANEL_COLOR);
		waitComponentJPanel.setOpaque(true);
		Util.setSize(waitComponentJPanel, 93, 15);
		
		this.initViews();
		this.initListener();
		this.setSize(450, 500);
		this.setLocationRelativeTo(null);
		
		queryAllTreeNode();
		this.setVisible(true);
	}
	
	
	
	private void initViews() {
		dataTProductCatalogTreePanel = new DataTProductCatalogTreePanel(jpb);
		dataTProductCatalogTreePanel.getTree().addMouseListener(this);
		dataTProductCatalogTreePanel.setCursor(Util.CURSOR_DEFAULT);
		JPanel jp100 = new BorderJPanel();
		noButton = new RPJButtonWhite("取消");
		yesButton = new RPJButtonWhite("确定");
		JLabel jlb=new JLabel("（双击选择）");
		jlb.setFont(new Font("宋体", Font.PLAIN, 12));
		jlb.setForeground(Color.RED);
		jp100.add(yesButton);
		jp100.add(noButton);
		jp100.add(jlb);
		jp100.setBackground(Util.PANEL_COLOR);
//		this.add(dataTProductCatalogTreePanel);
		this.add(waitComponentJPanel);
		this.add(jp100, BorderLayout.SOUTH);
	}
	private void initListener(){
		noButton.addActionListener(this);
		yesButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == noButton) {
			this.dispose();
		} else if (e.getSource() == yesButton) {
			treeNode = (OverwriteDefaultMutableTreeNode) dataTProductCatalogTreePanel.getTree().getLastSelectedPathComponent();
			if(treeNode!=null){
				nodeRow=dataTProductCatalogTreePanel.getTree().getLeadSelectionRow();
				this.dispose();
			}
		} else {
			super.actionPerformed(e);
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==dataTProductCatalogTreePanel.getTree()){
			if(e.getClickCount()==2){
				yesButton.doClick();
			}
		}
	}

	public int getNodeRow() {
		return nodeRow;
	}


	public OverwriteDefaultMutableTreeNode getTreeNode() {
		return treeNode;
	}
	
	//查询打开所有节点
	private void queryAllTreeNode() {
//		this.remove(dataTProductCatalogTreePanel);
//		this.add(waitComponentJPanel, BorderLayout.CENTER);
		
		dataTProductCatalogTreePanel.setDataCommodityQuery(this);
		queryCommodity();
	}
	
	@Override
	public void queryCommodity() {
		JTree tree = dataTProductCatalogTreePanel.getTree();
		while (queryAt < tree.getRowCount()) {
			if (tree.isCollapsed(queryAt) && !((OverwriteDefaultMutableTreeNode)tree.getPathForRow(queryAt).getLastPathComponent()).isLeaf()) {
				tree.expandRow(++queryAt-1);
					return;
			}
			++queryAt;
		}
		TreeNode root = (TreeNode)tree.getModel().getRoot();
		expandAll(tree, new TreePath(root), false);
		
//		this.remove(waitComponentJPanel);
//		this.add(dataTProductCatalogTreePanel, BorderLayout.CENTER);
		waitComponentJPanel.removeAll();
		waitComponentJPanel.add(dataTProductCatalogTreePanel);
		waitComponentJPanel.updateUI();
	}
	//关闭所有节点
	private void expandAll(JTree tree, TreePath parent, boolean expand) {
		TreeNode node = (TreeNode)parent.getLastPathComponent();
		if (node.getChildCount() >= 0) {
			for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
				TreeNode n = (TreeNode)e.nextElement();
				TreePath path = parent.pathByAddingChild(n);
				expandAll(tree, path, expand);
			}
		}
		if (expand) {
			tree.expandPath(parent);
		} else {
			tree.collapsePath(parent);
		}
	}
	
	public static void main(String[] args) {
		new DataTProductCatalogSelectDialog(null, "添加员工", true);
	}
}